package com.chinanet.utils.pon;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ZXGetFinalResult {

	
	public String returnSpeed(String txt) {
		String line = "\\-----------------------------------------------------";
		String[] a = returnMatcher(line, line, txt).trim().split("\\s+");
		try {
			return a[17];
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "无数据";
	}
	
	/**
	 * 获取光功率 ：1：RxPower 2:TxPower
	 * @param txt
	 * @param i
	 * @return
	 */
	public String returnLightPower(String txt,int i) {
		String line = "\\-----------------------------------------------------";
		String[] a = returnMatcher(line, line, txt).trim().split("\\s+");
		try {
			if(i==1){
				return a[14];
			}else if(i==2){
				return a[16];
			}
			} catch (Exception e) {
			e.printStackTrace();
		}
		return "无数据";
	}

	/**
	 * returnPtAndIpaddress
	 * @param txt
	 * @return
	 */
	public String[] returnPtAndIpaddress(String txt) {
		String b[]=new String[2];
		String line = "\\-----------------------------------------------------";
		String[] a = returnMatcher(line, line, txt).trim().split("\\s+");
		try {
			b[0]=a[21];
			b[1]=a[27];
		} catch (Exception e) {
			e.printStackTrace();
			b[0]="无数据";
			b[1]="无数据";
		}
		return b;
	}

	/**
	 * returnOUNstate
	 * @param txt
	 * @return
	 */
	public String returnOUNstate(String txt) {
		String line = "\\-----------------------------------------------------";
		String[] a = returnMatcher(line, line, txt).trim().split("\\s+");
		try {
			return a[9];
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "无数据";
	}

	/**
	 * returnUserNumber
	 * @param txt
	 * @return
	 */
	public String returnUserNumber(String txt) {
		String line = "\\-----------------------------------------------------";
		String[] a = returnMatcher(line, line, txt).trim().split("\\s+");
		try {	
				return a[19];
			} catch (Exception e) {
			e.printStackTrace();
		}
		return "无数据";
	}

	/**
	 * returnSoftwareVersion
	 * @param txt
	 * @return
	 */
	public String returnSoftwareVersion(String txt) {
		String line = "\\-----------------------------------------------------";
		String[] a = returnMatcher(line, line, txt).trim().split("\\s+");
		try {	
				return a[24];
			} catch (Exception e) {
			e.printStackTrace();
		}
		return "无数据";
	}

	/**
	 * returnServiceport_Rate
	 * @param txt
	 * @return
	 */
	public String[] returnServiceport_Rate(String txt) {
		String b[]=new String[3];
		String line = "\\-----------------------------------------------------";
		String[] a = returnMatcher(line, line, txt).trim().split("\\s+");
		try {
			b[0]=a[14];
			b[1]=a[15];
			b[2]=a[17];
		} catch (Exception e) {
			e.printStackTrace();
			b[0]="无数据";
			b[1]="无数据";
			b[2]="无数据";
		}
		return b;
	}

	/**
	 * returnVOIPINFO
	 * @param txt
	 * @return
	 */
	public String[] returnVOIPINFO(String txt) {
		String b[]=new String[5];
		String line = "\\-----------------------------------------------------";
		String[] a = returnMatcher(line, line, txt).trim().split("\\s+");
		try {
			b[0]=a[7];
			b[1]=a[8];
			b[2]=a[9];
			b[3]=a[10];
			b[4]=a[11];
		} catch (Exception e) {
			e.printStackTrace();
			b[0]="无数据";
			b[1]="无数据";
			b[2]="无数据";
			b[3]="无数据";
			b[4]="无数据";
		}
		return b;
	}

	/**
	 * returnOltCRC
	 * @param txt
	 * @param i
	 * @return
	 */
	public String returnOltCRC(String txt,int i) {
		String line = "\\-----------------------------------------------------";
		String[] a = returnMatcher(line, line, txt).trim().split("\\s+");
		try {
			if(i==1){
				return a[22];
			}else if(i==2){
				return a[21];
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "无数据";
	}
	
	/**
	 * 41bussinessbits
	 * @param txt
	 * @return
	 */
	public boolean returnisexist(String txt) {
		String line = "\\-----------------------------------------------------";
		String[] a = returnMatcher(line, line, txt).trim().split("\\s+");
		int n=9;
		int m=a.length/10;
		boolean flag=false;
//		List<String> UV=new ArrayList<String>();
		try {
			for(int i=0;i<m;i++){
				n=n+10;
				if(n<a.length){
//					UV.add(a[n]);
					if(a[n].trim().equals("41")){
						flag=true;
					}
				}
			}
//			for(String c:UV){
//				System.out.println(c);
//			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * dearUNIinfo
	 * @param txt
	 * @return
	 */
	public String dearUNIinfo(String txt) {
		String line = "\\-----------------------------------------------------";
		String[] a = returnMatcher(line, line, txt).trim().split("\\s+");
		try {
			if(a.length<=10){
				return "无数据";
			}else{
				String returnMsg="PORTID  ADMINSTATUS  DUPLEX  SPEED\r\n";
				for(int i=10;i<a.length;i++){
					if(i%10==4||i%10==5||i%10==6)
						returnMsg+=a[i]+"  ";
					if(i%10==7)
						returnMsg+=a[i]+"\r\n";
				}
				return returnMsg;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "无数据";
	}

	/**
	 *
	 * @param txt
	 * @return
	 */
	public String dear41_Bussinessbits(String txt) {
		String line = "\\-----------------------------------------------------";
		String[] a = returnMatcher(line, line, txt).trim().split("\\s+");
		try {
			if(a.length<=10){
				return "无数据";
			}else{
				String returnMsg="UV（业务流）	SVLAN（外层SVLAN）	CVLAN（内层CVLAN）\r\n";
				String insideMsg="";
				for(int i=10;i<a.length;i++){

					if(i%10==5)
						insideMsg+=a[i]+"			";
					if(i%10==6)
						insideMsg+=a[i]+"\r\n";
					if(i%10==9){
						insideMsg=a[i]+"			"+insideMsg;
						returnMsg+=insideMsg;
						insideMsg="";
					}
				}
				return returnMsg;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "无数据";
	}


	private String getalarmtxt(String txt){
		String returnBack = "";
		String start="PROPOSED_ADVISE";
		String line= "\\-----------------------------------------------------";
		returnBack = returnMatcher(start, line, txt);
		return returnBack;
	}
	/**
	 * do Alarm things
	 * @param txt
	 * @return
	 */
	public Map<String,Object> getPatterns_ALARM(String txt){
//		System.out.println(txt);
		Map<String,Object> out=new HashMap<String, Object>();
//		String line = "\\-----------------------------------------------------";
//		txt=returnMatcher(line, line, txt).trim();
		txt=getalarmtxt(txt);
		int Alarmfalg=0;
		String ONULosTime="";
		//M:ONU电源掉电
		int ONULoscount=0;

		String ONUdownTime="";
		//N:ONU离线
		int ONUdowncount=0;

		//OLT PON 光模块失效
		int OLTPONloscount=0;

		//检查ONU所在小区是否停电
		int XOUNLos=0;
		//如果PON口下只有一个ONU，那么检查分支光纤是否连接正常，否则检查主干光纤是否连接正常
		int mainline=0;

		int ONULosTimeflag=0;
		int ONUdownTimeflag=0;
		//PON口下多个ONT同时掉电
		String[] Pattentext=txt.split("RACK:");
		if(Pattentext.length>0) {
			List<List<String>> firstget = new ArrayList<List<String>>();
			int j = 0;
			for (String string : Pattentext) {
				if (j == 0) {
					List<String> one = new ArrayList<String>();
					String[] a = string.split("\t");
					for (int i = 1; i < a.length; i++)
						one.add(a[i]);
					firstget.add(one);
				} else {
					List<String> other = new ArrayList<String>();
					String[] a = string.split("\t");
					for (String here : a)
						other.add(here);
					firstget.add(other);
				}
				j++;
			}


			for (int i = 0; i < firstget.size() - 1; i++) {
				int end = 11;
				for (int z = 0; z < 11; z++) {
					firstget.get(i).add(firstget.get(i + 1).get(z));
				}

				while (0 < end) {
					firstget.get(i + 1).remove(0);
					end--;
				}
			}
			firstget.remove(firstget.size() - 1);


			for (List<String> list : firstget) {
				if (list.get(0).trim().indexOf("ONU电源掉电") != -1) {
					if (ONULoscount == 0) {
						ONULosTime = list.get(7).trim().substring(0, 16);
//				System.out.println("we"+list.get(7).trim().substring(0,16));
					} else {
						if (ONULosTime.equals(list.get(7).trim().substring(0, 16))) {
							ONULosTimeflag = 1;
						} else {
							ONULosTimeflag = 0;
						}
					}
					ONULoscount++;
				} else if (list.get(0).trim().indexOf("ONU离线") != -1) {
					if (ONUdowncount == 0) {
						ONUdownTime = list.get(7).trim().substring(0, 16);
//				System.out.println("you"+list.get(7).trim().substring(0,16));
					} else {
						if (ONUdownTime.equals(list.get(7).trim().substring(0, 16))) {
							ONUdownTimeflag = 1;
						} else {
							ONUdownTimeflag = 0;
						}

					}
					ONUdowncount++;
				} else if (list.get(0).trim().indexOf("OLT PON 光模块失效") != -1) {
					OLTPONloscount++;
				}
				if(list.get(list.size()-1).trim().indexOf("检查ONU所在小区是否停电")!=-1){
					XOUNLos++;
				}
				if(list.get(list.size()-1).trim().indexOf("如果PON口下只有一个ONU，那么检查分支光纤是否连接正常，否则检查主干光纤是否连接正常")!=-1){
					mainline++;
				}

			}
			/**
			 *
			 */

			if (txt.indexOf("OLT PON口收无光") != -1) {
				Alarmfalg = 1;
			}
		}
		out.put("Alarmfalg", Alarmfalg);
		out.put("ONULoscount", ONULoscount);
		out.put("ONULosTimeflag",ONULosTimeflag );
		out.put("ONUdowncount", ONUdowncount);
		out.put("ONUdownTimeflag", ONUdownTimeflag);
		out.put("OLTPONloscount", OLTPONloscount);
		out.put("XOUNLos", XOUNLos);
		out.put("mainline", mainline);


		System.out.println("Alarmfalg"+Alarmfalg);
		System.out.println("ONULoscount :"+ONULoscount);
		System.out.println("ONULosTimeflag :"+ONULosTimeflag);
		System.out.println("ONUdowncount :"+ONUdowncount);
		System.out.println("ONUdownTimeflag :"+ONUdownTimeflag);
		System.out.println("OLTPONlosflag"+OLTPONloscount);
		System.out.println("XOUNLos"+XOUNLos);
		System.out.println("mainline :"+mainline);
		return out;
	}

	/**
	 * returnMatcher
	 * @param leftPart
	 * @param rightPart
	 * @param txt
	 * @return
	 */
	public static String returnMatcher(String leftPart, String rightPart, String txt) {
		String returnBack = "";
		String regex = leftPart + "([\\s\\S].*)" + rightPart;
		Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE
				| Pattern.DOTALL);
		Matcher matcher = pattern.matcher(txt);

		while (matcher.find()) {
			returnBack = matcher.group(1);
		}

		return returnBack;
	}

}
